//taken from ndstool and modified trivially
//http://devkitpro.svn.sourceforge.net/viewvc/devkitpro/trunk/tools/nds/ndstool/source/encryption.cpp?revision=1565

/* decrypt.cpp - this file is part of DeSmuME
 *
 * Copyright (C) 2006 Rafael Vuijk
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This file is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "decrypt.h"

#include <stdlib.h>
#include <string.h>

#include "crc.h"
#include "header.h"

//encr_data
const unsigned char arm7_key[] =
{
	0x99,0xD5,0x20,0x5F,0x57,0x44,0xF5,0xB9,0x6E,0x19,0xA4,0xD9,0x9E,0x6A,0x5A,0x94,
	0xD8,0xAE,0xF1,0xEB,0x41,0x75,0xE2,0x3A,0x93,0x82,0xD0,0x32,0x33,0xEE,0x31,0xD5,
	0xCC,0x57,0x61,0x9A,0x37,0x06,0xA2,0x1B,0x79,0x39,0x72,0xF5,0x55,0xAE,0xF6,0xBE,
	0x5F,0x1B,0x69,0xFB,0xE5,0x9D,0xF1,0xE9,0xCE,0x2C,0xD9,0xA1,0x5E,0x32,0x05,0xE6,
	0xFE,0xD3,0xFE,0xCF,0xD4,0x62,0x04,0x0D,0x8B,0xF5,0xEC,0xB7,0x2B,0x60,0x79,0xBB,
	0x12,0x95,0x31,0x0D,0x6E,0x3F,0xDA,0x2B,0x88,0x84,0xF0,0xF1,0x3D,0x12,0x7E,0x25,
	0x45,0x22,0xF1,0xBB,0x24,0x06,0x1A,0x06,0x11,0xAD,0xDF,0x28,0x8B,0x64,0x81,0x34,
	0x2B,0xEB,0x33,0x29,0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,0x3F,0x92,0xAF,0xF3,0xCA,0xAC,
	0xAF,0x4B,0xD9,0x94,0xC0,0x43,0xCA,0x81,0x0D,0x2F,0x48,0xA1,0xB0,0x27,0xD5,0xD2,
	0xEF,0x4B,0x05,0x85,0xA3,0xDE,0x4D,0x93,0x30,0x3C,0xF0,0xBB,0x4A,0x8F,0x30,0x27,
	0x4C,0xEB,0xE3,0x3E,0x64,0xED,0x9A,0x2F,0x3B,0xF1,0x82,0xF0,0xBA,0xF4,0xCF,0x7F,
	0x40,0xCB,0xB0,0xE1,0x7F,0xBC,0xAA,0x57,0xD3,0xC9,0x74,0xF2,0xFA,0x43,0x0D,0x22,
	0xD0,0xF4,0x77,0x4E,0x93,0xD7,0x85,0x70,0x1F,0x99,0xBF,0xB6,0xDE,0x35,0xF1,0x30,
	0xA7,0x5E,0x71,0xF0,0x6B,0x01,0x2D,0x7B,0x64,0xF0,0x33,0x53,0x0A,0x39,0x88,0xF3,
	0x6B,0x3A,0xA6,0x6B,0x35,0xD2,0x2F,0x43,0xCD,0x02,0xFD,0xB5,0xE9,0xBC,0x5B,0xAA,
	0xD8,0xA4,0x19,0x7E,0x0E,0x5D,0x94,0x81,0x9E,0x6F,0x77,0xAD,0xD6,0x0E,0x74,0x93,
	0x96,0xE7,0xC4,0x18,0x5F,0xAD,0xF5,0x19,
};

u32 card_hash[0x412];
int cardheader_devicetype = 0;
u32 global3_x00, global3_x04;	// RTC value
u32 global3_rand1;
u32 global3_rand3;

static u32 lookup(u32 *magic, u32 v)
{
	u32 a = (v >> 24) & 0xFF;
	u32 b = (v >> 16) & 0xFF;
	u32 c = (v >> 8) & 0xFF;
	u32 d = (v >> 0) & 0xFF;

	a = magic[a+18+0];
	b = magic[b+18+256];
	c = magic[c+18+512];
	d = magic[d+18+768];

	return d + (c ^ (b + a));
}

static void encrypt(u32 *magic, u32 *arg1, u32 *arg2)
{
	u32 a,b,c;
	a = *arg1;
	b = *arg2;
	for (int i=0; i<16; i++)
	{
		c = magic[i] ^ a;
		a = b ^ lookup(magic, c);
		b = c;
	}
	*arg2 = a ^ magic[16];
	*arg1 = b ^ magic[17];
}

static void decrypt(u32 *magic, u32 *arg1, u32 *arg2)
{
	u32 a,b,c;
	a = *arg1;
	b = *arg2;
	for (int i=17; i>1; i--)
	{
		c = magic[i] ^ a;
		a = b ^ lookup(magic, c);
		b = c;
	}
	*arg1 = b ^ magic[0];
	*arg2 = a ^ magic[1];
}

static void encrypt(u32 *magic, u64 &cmd)
{
	encrypt(magic, (u32 *)&cmd + 1, (u32 *)&cmd + 0);
}

static void decrypt(u32 *magic, u64 &cmd)
{
	decrypt(magic, (u32 *)&cmd + 1, (u32 *)&cmd + 0);
}

static void update_hashtable(u32* magic, u8 arg1[8])
{
	for (int j=0;j<18;j++)
	{
		u32 r3=0;
		for (int i=0;i<4;i++)
		{
			r3 <<= 8;
			r3 |= arg1[(j*4 + i) & 7];
		}
		magic[j] ^= r3;
	}

	u32 tmp1 = 0;
	u32 tmp2 = 0;
	for (int i=0; i<18; i+=2)
	{
		encrypt(magic,&tmp1,&tmp2);
		magic[i+0] = tmp1;
		magic[i+1] = tmp2;
	}
	for (int i=0; i<0x400; i+=2)
	{
		encrypt(magic,&tmp1,&tmp2);
		magic[i+18+0] = tmp1;
		magic[i+18+1] = tmp2;
	}
}

u32 arg2[3];

static void init2(u32 *magic, u32 a[3])
{
	encrypt(magic, a+2, a+1);
	encrypt(magic, a+1, a+0);
	update_hashtable(magic, (u8*)a);
}

static void init1(u32 cardheader_gamecode)
{
	memcpy(card_hash, &arm7_key, 4*(1024 + 18));
	arg2[0] = *(u32 *)&cardheader_gamecode;
	arg2[1] = (*(u32 *)&cardheader_gamecode) >> 1;
	arg2[2] = (*(u32 *)&cardheader_gamecode) << 1;
	init2(card_hash, arg2);
	init2(card_hash, arg2);
}

static void init0(u32 cardheader_gamecode)
{
	init1(cardheader_gamecode);
	encrypt(card_hash, (u32*)&global3_x04, (u32*)&global3_x00);
	global3_rand1 = global3_x00 ^ global3_x04;		// more RTC
	global3_rand3 = global3_x04 ^ 0x0380FEB2;
	encrypt(card_hash, (u32*)&global3_rand3, (u32*)&global3_rand1);
}

// ARM9 decryption check values
#define MAGIC30		0x72636E65
#define MAGIC34		0x6A624F79

/*
 * decrypt_arm9
 */
static bool decrypt_arm9(u32 cardheader_gamecode, unsigned char *data)
{
	u32 *p = (u32*)data;

	init1(cardheader_gamecode);
	decrypt(card_hash, p+1, p);
	arg2[1] <<= 1;
	arg2[2] >>= 1;
	init2(card_hash, arg2);
	decrypt(card_hash, p+1, p);

	if (p[0] != MAGIC30 || p[1] != MAGIC34)
	{
		fprintf(stderr, "Decryption failed!\n");
		return false;
	}

	*p++ = 0xE7FFDEFF;
	*p++ = 0xE7FFDEFF;
	u32 size = 0x800 - 8;
	while (size > 0)
	{
		decrypt(card_hash, p+1, p);
		p += 2;
		size -= 8;
	}

	return true;
}

static void encrypt_arm9(u32 cardheader_gamecode, unsigned char *data)
{
	u32 *p = (u32*)data;
	if (p[0] != 0xE7FFDEFF || p[1] != 0xE7FFDEFF)
	{
		fprintf(stderr, "Encryption failed!\n");
		return;
	}
	p += 2;

	init1(cardheader_gamecode);

	arg2[1] <<= 1;
	arg2[2] >>= 1;
	
	init2(card_hash, arg2);

	u32 size = 0x800 - 8;
	while (size > 0)
	{
		encrypt(card_hash, p+1, p);
		p += 2;
		size -= 8;
	}

	p = (u32*)data;
	p[0] = MAGIC30;
	p[1] = MAGIC34;
	encrypt(card_hash, p+1, p);
	init1(cardheader_gamecode);
	encrypt(card_hash, p+1, p);
}

//the NDS contains 
//0x0000 - 0x01FF : header
//0x0200 - 0x3FFF : typically, nothing is stored here. on retail cards, you can't read from that area anyway, but im not sure if that's done in the game card or the GC bus controller on the system
//0x4000 - 0x7FFF : secure area (details in gbatek)

bool DecryptSecureArea(u8 *romheader, u8 *secure)
{
	//this looks like it will only work on little endian hosts
	Header* header = (Header*)romheader;

	int romType = DetectRomType(*header, (char*)secure);

	if(romType == ROMTYPE_INVALID)
		return false;

	// check if ROM is already encrypted
	if (romType == ROMTYPE_NDSDUMPED)
	{
		printf("Already decrypted.\n");
	}
	else if (romType >= ROMTYPE_ENCRSECURE)		// includes ROMTYPE_MASKROM
	{
		//unsigned char data[0x4000];
		//memcpy(data,romdata+0x4000,0x4000);
		//decrypt_arm9(*(u32 *)header->gamecode, data);
		//// clear data after header
		//memset(romdata+0x200,0,(0x4000-0x200));
		//// write secure 0x800
		//memcpy(romdata+0x4000,data,0x800);

		if (!decrypt_arm9(*(u32 *)header->gamecode, secure))
			return false;

		printf("Decrypted.\n");
	}
	else
	{
		printf("File doesn't appear to have a secure area.\n");
	}

	return true;
}

bool EncryptSecureArea(u8 *romheader, u8 *secure)
{
	//this looks like it will only work on little endian hosts
	Header* header = (Header*)romheader;

	int romType = DetectRomType(*header, (char*)secure);

	if(romType == ROMTYPE_INVALID)
		return false;

	if (romType == ROMTYPE_NDSDUMPED)
	{
		//unsigned char data[0x4000];
		//memcpy(data,romdata+0x4000,0x4000);
		//encrypt_arm9(*(u32 *)header->gamecode, data);
		//// clear data after header
		//memset(romdata+0x200,0,(0x4000-0x200));
		//// write secure 0x800
		//memcpy(romdata+0x4000,data,0x800);

		encrypt_arm9(*(u32 *)header->gamecode, secure);

		printf("Encrypted.\n");
	}

	return true;
}

bool CheckValidRom(u8 *header, u8 *secure)
{
	Header* hdr = (Header*)header;

	int romType = DetectRomType(*hdr, (char*)secure);

	return (romType != ROMTYPE_INVALID);
}